package com.ketai.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.ketai.dao.ApplicationDao;
import com.ketai.dao.impl.ApplicationDaoImpl;
import com.ketai.service.ApplicationService;
import com.ketai.util.Pages;
import com.ketai.vo.ActivityVo;
import com.ketai.vo.ApplicationVo;

import javax.naming.NamingException;
import java.lang.reflect.Array;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:长沙科泰梦想学院-蓝色小药丸
 * @Mission:打造演说·创客型跨境电商与互联网人才
 * @Description:
 * @Date:Create In 2022/8/9 21:24
 **/
public class ApplicationServiceImpl implements ApplicationService {
    ApplicationDao appDao = new ApplicationDaoImpl();

    @Override
    public Pages<ApplicationVo> findPage (String title, String checked,String pageidnex, String limit) {
        Pages<ApplicationVo> result = new Pages<ApplicationVo>();

        StringBuilder sb = new StringBuilder("select a.* ,c.title uname, c.time apply_time    from apply a , c_activity c where a.unid=c.id  ");
        String sql2 = "select count(1)  total from apply a , c_activity c where a.unid=c.id ";

        List params = new ArrayList<>();
        if (!StrUtil.isEmpty(title)) {
            sb.append("  and c.title like concat('%',?,'%')");
            sql2 += " and c.title like concat('%',?,'%')";
            params.add(title);
        }

        if (!StrUtil.hasEmpty(checked)) {
            sb.append(" and achecked=? ");
            sql2 +=" and achecked=? ";
            params.add(checked);
        }

        try {
          long total=  appDao.getApplicationCount(sql2, params.toArray());
            result.setDataTotal(total);
            int pageSize=limit==null?5: Convert.toInt(limit);
            int pagenum=pageidnex==null?1: Convert.toInt(pageidnex);
            result.setPageNumber(pageSize);
            int pages = (int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
            result.setTotalPages(pages);
            result.setCurrentPage(pagenum);
            sb.append("  order by a.application_time desc   limit ?,? ");

            params.add((pagenum - 1) * pageSize);
            params.add( pageSize);

            List<ApplicationVo> list = appDao.findAllApplication(sb.toString(), params.toArray());
            result.setDataItems(list);


        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }


        return result;
    }

    @Override
    public int checkedActity (String id, String open, String desc, String pid) {

        String sql="update apply set achecked=? ";
        List params = new ArrayList();
        if (!StrUtil.isBlank(open)&&open.equals("on")) {
            sql +=" ,  `detail`='' ";
            params.add(1);
        }else {
            sql+=" ,  `detail`=? ";
            params.add(2);
            params.add(desc);
        }
        sql += " , checkedTime=now() ,acheckedId=? where id=?";
        params.add(pid);
        params.add(id);
        System.out.println(sql);
        try {
            return  appDao.checkedActity(sql, params.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }

        return 0;
    }
}
